package com.kanshu.ksgb.fastread.doudou.module.reader.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.kanshu.ksgb.fastread.doudou.R;

/**
 * created by wcy
 * 阅读器菜单栏底部控件
 */
public class BookReadBottomLayout extends LinearLayout {
    public static final int TAB_READER_PROGRESS = 0;
    public static final int TAB_READER_BRIGHTNESS = 1;
    public static final int TAB_READER_BOOKMARK = 2;
    public static final int TAB_READER_SETTINGS = 3;
    IOnReaderTabChangeListener mCallback;
    public BookReadBottomLayout(Context context) {
        super(context);
        init();
    }

    public BookReadBottomLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BookReadBottomLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOrientation(LinearLayout.HORIZONTAL);
        setGravity(Gravity.CENTER);
        setClickable(true);
        LayoutInflater.from(getContext()).inflate(R.layout.layout_reader_bottom_bar, this);
        for (int i =0 ; i < getChildCount(); i++) {
            final int tabPos = i;
            getChildAt(i).setOnClickListener(v -> {
                clearCheck();
                v.setSelected(true);
                if (mCallback != null) {
                    mCallback.onTabChange(tabPos);
                }
            });
        }
    }

    /**
     * 清除选项
     */
    public void clearCheck() {
        for (int i = 0; i < getChildCount(); i++) {
            getChildAt(i).setSelected(false);
        }
    }

    /**
     * 设置tab其中一个位置为选中状态
     * @param pos
     */
    public void setCheck(int pos) {
        if (pos >= getChildCount()) {
            return;
        }
        getChildAt(pos).setSelected(true);
    }

    /**
     * 让setting为选中状态
     */
    public void setSettingsCheck() {
        setCheck(TAB_READER_SETTINGS);
    }

    public void setTabChangeCallback(IOnReaderTabChangeListener callback) {
        mCallback = callback;
    }

    public interface IOnReaderTabChangeListener {
        void onTabChange(int tabPos);
    }
}
